<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-row>
          <el-button type="success" @click="handleCreate">&emsp;新增&emsp;</el-button>
          <el-button type="danger" @click="handleDeleteAll">批量删除</el-button>

        </el-row>
      </div>
      <div v-loading="listLoading" class="item">
        <el-table ref="dataList" :data="menuList" :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
                  border row-key="id"
                  style="width:100%;margin-bottom: 20px;"
                  @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection"></el-table-column>
          <el-table-column label="ID" prop="id"></el-table-column>
          <el-table-column label="等级" prop="grade"></el-table-column>
          <el-table-column label="等级名称" prop="title"></el-table-column>
          <el-table-column label="小图标" prop="head_picture">
            <template slot-scope="scope">
              <el-popover max-width="420" placement="right" trigger="hover">
                <img :src="scope.row.head_picture" alt="" style="max-width: 200px;max-height: 200px">
                <img slot="reference" :src="scope.row.head_picture" alt="" style="width: 50px;">
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column label="大图标" prop="main_icon">
            <template slot-scope="scope">
              <el-popover max-width="420" placement="right" trigger="hover">
                <img :src="scope.row.main_icon" alt="" style="max-width: 200px;max-height: 200px">
                <img slot="reference" :src="scope.row.main_icon" alt="" style="width: 50px;">
              </el-popover>
            </template>
          </el-table-column>

          <el-table-column label="图标背景" prop="basics_icon">
            <template slot-scope="scope">
              <el-popover max-width="420" placement="right" trigger="hover">
                <img :src="scope.row.basics_icon" alt="" style="max-width: 200px;max-height: 200px">
                <img slot="reference" :src="scope.row.basics_icon" alt="" style="width: 50px;">
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column label="详情图" prop="large_icon">
            <template slot-scope="scope">
              <el-popover max-width="420" placement="right" trigger="hover">
                <img :src="scope.row.large_icon" alt="" style="max-width: 200px;max-height: 200px">
                <img slot="reference" :src="scope.row.large_icon" alt="" style="width: 50px;">
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column label="经验值" prop="experience"></el-table-column>
          <el-table-column label="升级横幅" prop="banner">
            <template slot-scope="scope">
              <el-popover max-width="420" placement="right" trigger="hover">
                <img :src="scope.row.banner" alt="" style="max-width: 200px;max-height: 200px">
                <img slot="reference" :src="scope.row.banner" alt="" style="width: 50px;">
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column label="升级图动效" prop="special">
            <template slot-scope="scope">
              <el-popover max-width="420" placement="right" trigger="hover">
                <img :src="scope.row.special" alt="" style="max-width: 200px;max-height: 200px">
                <img slot="reference" :src="scope.row.special" alt="" style="width: 50px;">
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button icon="el-icon-edit" plain size="mini" type="primary" @click="handleUpdate(scope.row)">编辑
              </el-button>
              <el-button icon="el-icon-delete" plain size="mini" type="danger" @click="handleDelete(scope.row)">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <!--        <cus-pagination-->
        <!--          :limit.sync="listQuery.perPage"-->
        <!--          :page.sync="listQuery.page"-->
        <!--          :total="total"-->
        <!--          @pagination="getList"-->
        <!--        />-->
      </div>
    </el-card>
    <!-- 删除弹框 -->
    <el-dialog
      :visible.sync="deleteDialogVisible"
      title="确定删除吗?"
      width="435px"
    >
      <span style=""><em style="font-style:normal;color:red">删除后不可恢复</em>，你还要继续吗？</span>
      <span slot="footer" class="dialog-footer">
            <el-button @click="deleteDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="confirmDelete">确 定</el-button>
        </span>
    </el-dialog>
    <!-- 新增编辑弹框 -->
    <el-dialog v-dialogDrag :title="titleMap[dialogStatus]" :visible.sync="dialogFormVisible"
               @close="handleDialogClose('editRulesForm')"
    >
      <el-form ref="editRulesForm" :model="editRulesForm" :rules="editRulesFormRules" class="demo-ruleForm"
               label-width="120px"
      >
        <el-form-item label="等级" prop="grade">
          <el-input v-model="editRulesForm.grade" placeholder="请输入等级"></el-input>
        </el-form-item>
        <el-form-item label="等级名称" prop="title">
          <el-input v-model="editRulesForm.title" placeholder="请输入等级名称"></el-input>
        </el-form-item>
        <el-form-item label="经验值" prop="experience">
          <el-input v-model="editRulesForm.experience" placeholder="请输入经验值"></el-input>
        </el-form-item>
        <el-form-item label="小图标" prop="head_picture">
          <el-upload
            ref="upload"
            :auto-upload="true"
            :file-list="specialFlagBigList"
            :http-request="customizeUpload"
            :on-change="handleChangePic"
            :on-remove="handleSpecialFlagBigRemove"
            :on-success="handleSpecialFlagBigSuccess"
            action=""
            class="upload-demo"
            list-type="picture"
          >
            <el-button slot="trigger" size="small" type="primary">上传小图标</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="大图标" prop="main_icon">
          <el-upload
            ref="upload"
            :auto-upload="true"
            :file-list="specialFlagBigList2"
            :http-request="customizeUpload"
            :on-change="handleChangePic"
            :on-remove="handleSpecialFlagBigRemove2"
            :on-success="handleSpecialFlagBigSuccess2"
            action=""
            class="upload-demo"
            list-type="picture"
          >
            <el-button slot="trigger" size="small" type="primary">上传大图标</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="图标背景" prop="basics_icon">
          <el-upload
            ref="upload"
            :auto-upload="true"
            :file-list="specialFlagBigList3"
            :http-request="customizeUpload"
            :on-change="handleChangePic"
            :on-remove="handleSpecialFlagBigRemove3"
            :on-success="handleSpecialFlagBigSuccess3"
            action=""
            class="upload-demo"
            list-type="picture"
          >
            <el-button slot="trigger" size="small" type="primary">上传图标背景</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="详情图" prop="large_icon">
          <el-upload
            ref="upload"
            :auto-upload="true"
            :file-list="specialFlagBigList4"
            :http-request="customizeUpload"
            :on-change="handleChangePic"
            :on-remove="handleSpecialFlagBigRemove4"
            :on-success="handleSpecialFlagBigSuccess4"
            action=""
            class="upload-demo"
            list-type="picture"
          >
            <el-button slot="trigger" size="small" type="primary">上传详情图</el-button>
          </el-upload>
        </el-form-item>

        <el-form-item label="升级横幅" prop="banner">
          <el-upload
            ref="upload"
            :auto-upload="true"
            :file-list="specialFlagBigList5"
            :http-request="customizeUpload"
            :on-change="handleChangePic"
            :on-remove="handleSpecialFlagBigRemove5"
            :on-success="handleSpecialFlagBigSuccess5"
            action=""
            class="upload-demo"
            list-type="picture"
          >
            <el-button slot="trigger" size="small" type="primary">上传详情图</el-button>
          </el-upload>
        </el-form-item>

        <el-form-item label="基本文本颜色" prop="base">
          <el-input v-model="editRulesForm.base" placeholder="请输入"></el-input>
        </el-form-item>

        <el-form-item label="动态文本颜色" prop="dynamic">
          <el-input v-model="editRulesForm.dynamic" placeholder="请输入"></el-input>
        </el-form-item>

        <!--        <el-form-item-->
        <!--          v-for="(domain, index) in editRulesForm.domains"-->
        <!--          :key="domain.key"-->
        <!--          :label="'域名' + index"-->
        <!--          :prop="'domains.' + index + '.value'"-->
        <!--          :rules="{-->
        <!--              required: true, message: '不能为空', trigger: 'blur'-->
        <!--          }"-->
        <!--        >-->
        <!--          <el-input v-model="domain.value"></el-input>-->
        <!--          <el-button @click.prevent="removeDomain(domain)">删除</el-button>-->
        <!--          <el-button v-if="editRulesForm.domains.length<2" @click="addDomain">新增</el-button>-->
        <!--        </el-form-item>-->

        <el-form-item label="升级图动效" prop="special">
          <el-upload
            ref="upload"
            :auto-upload="true"
            :file-list="specialFlagBigList6"
            :http-request="customizeUpload"
            :on-change="handleChangePic"
            :on-remove="handleSpecialFlagBigRemove6"
            :on-success="handleSpecialFlagBigSuccess6"
            action=""
            class="upload-demo"
            list-type="picture"
          >
            <el-button slot="trigger" size="small" type="primary">上传详情图</el-button>
          </el-upload>
        </el-form-item>


        <el-form-item>
          <el-button @click="dialogFormVisible = false">返回</el-button>
          <el-button type="primary" @click="submitForm('editRulesForm')">提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { addWealth, delWealth, editWealth, listData } from '@/api/userLevelManage/wealthLevel'

export default {
  data() {
    return {
      confirmBtn: false,
      specialFlagBigList: [],
      specialFlagBigList2: [],
      specialFlagBigList3: [],
      specialFlagBigList4: [],
      specialFlagBigList5: [],
      specialFlagBigList6: [],
      // 要修改的当前行的id
      row_id: 0,
      provinceList: [],
      cityList: [],
      // 表单规则
      editRulesFormRules: {
        grade: [{ required: true, message: '请输入等级', trigger: 'blur' }],
        experience: [{ required: true, message: '请输入经验', trigger: 'blur' }],
        title: [{ required: true, message: '请输入等级名称', trigger: 'blur' }],
        main_icon: [{ required: true, message: '请上传大图标', trigger: 'blur' }],
        // basics_icon: [{ required: true, message: '请上背景图', trigger: 'blur' }],
        head_picture: [{ required: true, message: '请上传小图标', trigger: 'blur' }]
      },

      editRulesForm: {
        id: undefined,
        grade: undefined,
        experience: undefined,
        head_picture: undefined,
        title: undefined,
        main_icon: undefined,
        basics_icon: undefined,
        large_icon: undefined,
        dynamic: undefined,
        base: undefined,
        banner: undefined,
        special: undefined
      },
      dialogStatus: '',
      // dialog的头部信息
      titleMap: {
        update: '编辑',
        create: '添加'
      },
      dialogFormVisible: false,
      deleteDialogVisible: false,
      listLoading: false,
      menuList: [],
      limitList: [],
      listQuery: {
        page: 1,
        perPage: 10
      },
      total: 0,
      // 勾选内容
      multipleSelection: [],
      // 要删除的内容的ids
      dataIds: ''
    }
  },
  created() {
    this.getList()
  },
  methods: {
    addDomain() {
      this.editRulesForm.domains.push({
        value: '',
        key: Date.now()
      })
    },
    removeDomain(item) {
      let index = this.editRulesForm.domains.indexOf(item)
      if (index !== -1) {
        this.editRulesForm.domains.splice(index, 1)
      }
    },
    handleChangePic(fileList) {
      if (fileList.length > 1) {
        fileList.splice(0, 1)
      }
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/png' || file.type === 'image/webp' || file.type === 'image/gif'
      const isLt2M = file.size / 1024 / 1024 < 2

      // if (!isJPG) {
      //   this.$message.error('请上传JPG/PNG格式的图片!')
      // }
      // if (!isLt2M) {
      //   this.$message.error('上传图片大小不能超过 2MB!')
      // }
      return isJPG && isLt2M
    },
    handleSpecialFlagBigSuccess() {
      this.editRulesForm.head_picture = this.ossLastImgUrl
      this.specialFlagBigList = [{ url: this.ossLastImgUrl }]
    },
    handleSpecialFlagBigRemove() {
      this.editRulesForm.head_picture = ''
      this.specialFlagBigList = []
    },
    handleSpecialFlagBigSuccess2() {
      this.editRulesForm.main_icon = this.ossLastImgUrl
      this.specialFlagBigList2 = [{ url: this.ossLastImgUrl }]
    },
    handleSpecialFlagBigRemove2() {
      this.editRulesForm.main_icon = ''
      this.specialFlagBigList2 = []
    },
    handleSpecialFlagBigSuccess3() {
      this.editRulesForm.basics_icon = this.ossLastImgUrl
      this.specialFlagBigList3 = [{ url: this.ossLastImgUrl }]
    },
    handleSpecialFlagBigRemove3() {
      this.editRulesForm.basics_icon = ''
      this.specialFlagBigList3 = []
    },
    handleSpecialFlagBigSuccess4() {
      this.editRulesForm.large_icon = this.ossLastImgUrl
      this.specialFlagBigList4 = [{ url: this.ossLastImgUrl }]
    },
    handleSpecialFlagBigRemove4() {
      this.editRulesForm.large_icon = ''
      this.specialFlagBigList4 = []
    },
    handleSpecialFlagBigSuccess5() {
      this.editRulesForm.banner = this.ossLastImgUrl
      this.specialFlagBigList5 = [{ url: this.ossLastImgUrl }]
    },
    handleSpecialFlagBigRemove5() {
      this.editRulesForm.banner = ''
      this.specialFlagBigList5 = []
    },
    handleSpecialFlagBigSuccess6() {
      this.editRulesForm.special = this.ossLastImgUrl
      this.specialFlagBigList6 = [{ url: this.ossLastImgUrl }]
    },
    handleSpecialFlagBigRemove6() {
      this.editRulesForm.special = ''
      this.specialFlagBigList6 = []
    },
    getList() {
      this.listLoading = true
      listData().then((response) => {
        this.menuList = response.data
        this.listLoading = false
        this.confirmBtn = false
      })
    },
    // 点击新增
    handleCreate() {
      this.resetForm('editRulesForm')
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    // 点击编辑
    handleUpdate(row) {
      // row)
      this.row_id = row.id
      this.editRulesForm = {
        id: row.id,
        grade: row.grade,
        experience: row.experience,
        head_picture: row.head_picture,
        title: row.title,
        main_icon: row.main_icon,
        basics_icon: row.basics_icon,
        large_icon: row.large_icon,
        dynamic: JSON.parse(row.banner_color).dynamic && JSON.parse(row.banner_color).dynamic,
        base: JSON.parse(row.banner_color).base && JSON.parse(row.banner_color).base,
        banner: row.banner,
        special: row.special
      }
      this.specialFlagBigList = row.head_picture ? [{ url: row.head_picture }] : []
      this.specialFlagBigList2 = row.main_icon ? [{ url: row.main_icon }] : []
      this.specialFlagBigList3 = row.basics_icon ? [{ url: row.basics_icon }] : []
      this.specialFlagBigList4 = row.large_icon ? [{ url: row.large_icon }] : []
      this.specialFlagBigList5 = row.banner ? [{ url: row.banner }] : []
      this.specialFlagBigList6 = row.special ? [{ url: row.special }] : []
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
    },
    // 重置表单内容
    resetForm(formName) {
      this.$nextTick(() => {
        // this.$refs[formName].resetFields();
        if (this.$refs[formName] !== undefined) {
          this.$refs[formName].resetFields()
        }
        this.editRulesForm = {
          id: undefined,
          grade: undefined,
          experience: undefined,
          head_picture: undefined,
          title: undefined,
          main_icon: undefined,
          basics_icon: undefined,
          large_icon: undefined,
          dynamic: undefined,
          base: undefined,
          banner: undefined,
          special: undefined

        }
      })

      this.specialFlagBigList = []
      this.specialFlagBigList2 = []
      this.specialFlagBigList3 = []
      this.specialFlagBigList4 = []
      this.specialFlagBigList5 = []
      this.specialFlagBigList6 = []
    },
    // 勾选
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    submitForm(formName) {
      if (this.confirmBtn) return

      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.confirmBtn = true
          this.editRulesForm.banner_color = JSON.stringify({
            'base': this.editRulesForm.base,
            'dynamic': this.editRulesForm.dynamic
          })
          if (this.dialogStatus === 'create') {
            addWealth(this.editRulesForm).then(response => {
              if (response.code === 200) {
                this.getList()
                this.dialogFormVisible = false
                this.$notify({
                  type: 'success',
                  message: '操作成功',
                  position: 'bottom-right',
                  showClose: false,
                  duration: 3000
                })
              } else {
                this.confirmBtn = false
              }
            })
          } else {
            editWealth(this.editRulesForm).then(response => {
              if (response.code === 200) {
                this.getList()
                this.dialogFormVisible = false
                this.$notify({
                  type: 'success',
                  message: '操作成功',
                  position: 'bottom-right',
                  showClose: false,
                  duration: 3000
                })
              } else {
                this.confirmBtn = false
              }
            })
          }
        } else {
          // 'error submit!!');
          return false
        }
      })
    },
    // 点击单个删除
    handleDelete(row) {
      this.dataIds = { ids: `${row.id}` }
      this.deleteDialogVisible = true
    },
    // 点击批量删除
    handleDeleteAll() {
      if (this.multipleSelection.length === 0) {
        this.$message({
          message: '请先勾选要删除的内容',
          type: 'warning'
        })
        return
      } else if (this.multipleSelection.length !== 0) {
        let ids = ''
        for (let key in this.multipleSelection) {
          ids += `${this.multipleSelection[key].id},`
        }
        ids = ids.substring(0, ids.length - 1)
        this.dataIds = { ids: ids }
      }
      this.deleteDialogVisible = true
    },
    // 确定删除
    confirmDelete() {
      if (this.confirmBtn) return
      this.confirmBtn = true
      // this.dataIds)
      delWealth(this.dataIds).then((response) => {
        if (response.code === 200) {
          this.getList()
          this.deleteDialogVisible = false
          this.$notify({
            type: 'success',
            message: '操作成功',
            position: 'bottom-right',
            showClose: false,
            duration: 3000
          })
        } else {
          this.confirmBtn = false
        }
      })
    },
    // 监听dialog关闭时的处理事件
    handleDialogClose(formName) {
      if (this.$refs[formName]) {
        this.$refs[formName].clearValidate() // 清除整个表单的校验
      }
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep {
  .el-upload-list__item {
    transition : none !important;
  }

  .el-upload-list--picture {
    max-height : 103px !important;
    overflow   : hidden;
  }
}
</style>
